import 'package:flutter/material.dart';
import '../provide/details_info.dart';
import 'package:provide/provide.dart';
import '../details_page/details_top_area.dart';
import '../details_page/details_explain.dart';
import '../details_page/details_tabBar.dart';
import '../details_page/details_web.dart';
import '../details_page/details_bottom.dart';
import '../details_page/details_input.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class DetailsPage extends StatefulWidget {
  final String goodsId;

  DetailsPage({Key key,@required this.goodsId}) : super(key: key);

  _DetailsPageState createState() => _DetailsPageState();
}

class _DetailsPageState extends State<DetailsPage> {

  @override
  void deactivate() {
    // TODO: implement deactivate
    super.deactivate();
    Provide.value<DetailsInfoProvide>(context).changeLeftAndRight('left');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: (){
            Navigator.pop(context);
          },
        ),
        title: Text('商品详情'),
      ),
      body: FutureBuilder(
        future: _getBackInfo(context),
        builder: (context,snapshot){
          if(snapshot.hasData){
            return Stack(
              children: <Widget>[
                ListView(
                  // 给 80 padding 防止底部购买按钮覆盖商品图片
                  padding: EdgeInsets.only(bottom: ScreenUtil().setHeight(80)),
                  children: <Widget>[
                    DetailsTopArea(),
                    DetailsExplain(),
                    DetailsTabbar(),
                    DetailsWeb(),                          
                  ],
                  ),
                Positioned(
                  bottom: 0,
                  left: 0,
                  child: Provide<DetailsInfoProvide>(
                    builder: (context,child,val){
                      var isLeft = Provide.value<DetailsInfoProvide>(context).isLeft;
                      if(isLeft){
                        return DetailsBottom();
                      }else {
                        return BottomInput();
                      }
                    },
                  ) 
                )
              ],
            );
          }else {
            return Center(
              child: Text('加载中'),
            );
          }
        },
      ),
    );
  }

  Future _getBackInfo(BuildContext context )async{
      // 获取商品详细信息
      print('获取商品详情');
      await  Provide.value<DetailsInfoProvide>(context).getGoodsInfo(widget.goodsId);
      // await  Provide.value<DetailsInfoProvide>(context).changeLeftAndRight('left');
      print('获取商品数据完毕');
      return '完成加载';
  }

}